package com.example.expandablelistview.parser;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.ListView;

import com.example.expandablelistview.R;

public class Sax_parserAcitivity extends Activity {
	ListView list;
	ProgressDialog progressDlg;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.network_xmlparser);
		list = (ListView) findViewById(R.id.listView);
		new ParsingXml().execute("order.xml");
	}

	private class ParsingXml extends AsyncTask<String, Void, ArrayList<Item>> {
		protected void onPreExecute() {
			progressDlg = ProgressDialog.show(Sax_parserAcitivity.this, "Wait",
					"order.xml & order.json Making ...");
		}

		protected void onPostExecute(ArrayList<Item> result) {
			MyAdapter adapter = new MyAdapter(Sax_parserAcitivity.this,
					R.layout.itemrow, result);
			progressDlg.dismiss();
			list.setAdapter(adapter);
		}

		protected ArrayList<Item> doInBackground(String... params) {
			StringBuffer sb = new StringBuffer();
			String str = null;
			SaxHandler handler = null;
			try {
				BufferedReader fis = new BufferedReader(new InputStreamReader(
						openFileInput(params[0])));
				while ((str = fis.readLine()) != null) {
					sb.append(str);
				}
				fis.close();

				SAXParserFactory factory = SAXParserFactory.newInstance();
				SAXParser parser = factory.newSAXParser();
				XMLReader reader = parser.getXMLReader();
				handler = new SaxHandler();
				reader.setContentHandler(handler);
				InputStream istream = new ByteArrayInputStream(sb.toString()
						.getBytes("utf-8"));
				reader.parse(new InputSource(istream));

			} catch (Exception e) {
				e.printStackTrace();
			}
			return handler.arItem;
		}
	}

	private class SaxHandler extends DefaultHandler {
		boolean initem = false;
		Item itemClass;
		ArrayList<Item> arItem = new ArrayList<Item>();

		public void startDocument() {
		}

		public void endDocument() {
		}

		public void startElement(String uri, String localName, String qName,
				Attributes atts) {
			if (localName.equals("item")) {
				initem = true;
				itemClass = new Item();
			}

			if (atts.getLength() > 0) {
				for (int i = 0; i < atts.getLength(); i++) {
					if (atts.getLocalName(i).equalsIgnoreCase("price")) {
						itemClass.setItemPrice(Integer.parseInt(atts
								.getValue(i)));
					} else {
						itemClass.setMakerName(atts.getValue(i));
					}
				}
			}
		}

		public void endElement(String uri, String localName, String qName) {
		}

		public void characters(char[] chars, int start, int length) {
			if (initem) {
				itemClass.setItemName(new String(chars, start, length)
						.toString());
				arItem.add(itemClass);
				initem = false;
			}
		}
	}

}
